import HeadPrefab from "../../../component/HeadPrefab";
import ViewBase from "../../../engine/ViewBase";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { UIHelper } from "../../util/UIHelper";
import ViewClubMemberDetail from "./ViewClubMemberDetail";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubScore extends ViewBase {


    private data: JhaoProto.IClub_User;
    private parent: ViewClubMemberDetail

    private toggleContainer: cc.ToggleContainer;

    public onInitChild(data: JhaoProto.IClub_User, parent: ViewClubMemberDetail) {
        this.data = data;
        this.parent = parent

        // this.refresh()
    }

    onEnable(): void {
        super.onEnable()
        this.toggleContainer = this.getChild(this.node, "toggleContainer", cc.ToggleContainer)
        this.setToggleContainerIndex(this.toggleContainer, 0)
        this.refresh()
        let btnGive: cc.Node = this.getChild(this.node, "btnGive")
        let txtGiveNum: cc.EditBox = this.getChild(this.node, "txtGiveNum", cc.EditBox)
        UIHelper.onAddEvent(btnGive, () => {
            let index: number = this.getToggleContainerIndex(this.toggleContainer)
            let giveNum: number = parseInt(txtGiveNum.string)
            if (index == 1) {
                giveNum = -giveNum
            }
            ClubSocketClient.req_club_score(this.data.clubid, this.data.userid, giveNum, ()=>{
                this.data.score += giveNum
                this.parent.refresh()
                this.closeView()
            })
        })
    }

    private refresh() {
        let head: HeadPrefab = this.getChild(this.node, "head", HeadPrefab)
        head.setHead(this.data.headimgurl)
        let txtName: cc.Label = this.getChild(this.node, "txtName", cc.Label)
        txtName.string = this.data.nickName
        let txtId: cc.Label = this.getChild(this.node, "txtId", cc.Label)
        txtId.string = "ID:" + this.data.userid
    }
}
